package com.csizg.facerecognition;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.csizg.facemodel.util.CompareResult;
import com.csizg.facemodel.util.FaceFileUtil;
import com.csizg.facemodel.util.FaceUtil;
import com.csizg.facemodel.view.ICompareListener;
import com.csizg.facemodel.view.IFaceShowListener;
import com.csizg.facemodel.view.MyFaceCameraView;
import com.csizg.facemodel.view.MyNDFaceCameraView;
import com.csizg.facemodel.view.RectBitmap;

import org.opencv.android.CameraBridgeViewBase;

public class FdVR1NDActivity extends Activity {

    private Context mContext;
    private MyNDFaceCameraView mOpenCvCameraView;
    private TextView mSaveTextView;
    private static String SET_TAG = "tag";
    private int isFaceDetct ;
    private boolean mFaceOpened =false;
    private static FaceResultListener mFaceResultListener;

    private SharedPreferences sp;

    public static void setFaceListener(FaceResultListener faceResultListener) {
        mFaceResultListener = faceResultListener;
    }

    public interface FaceResultListener {
        void faceDetcetOk();

        void faceDetcetError();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_face_detct);
        TextView titleText = (TextView) findViewById(R.id.tv_title_text);

        sp = getSharedPreferences("face_reco",Context.MODE_PRIVATE);
        mFaceOpened = sp.getBoolean("open",false);
        titleText.setText("人脸采集");

        if(mFaceOpened){
            titleText.setText("人脸识别");
        }

        ImageView titleBack = (ImageView) findViewById(R.id.iv_go_back);
        titleBack.setVisibility(View.VISIBLE);
        titleBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mOpenCvCameraView = findViewById(R.id.face_activity_surface_view);
        mOpenCvCameraView.setVisibility(CameraBridgeViewBase.VISIBLE);
        mOpenCvCameraView.setCameraIndex(CameraBridgeViewBase.CAMERA_ID_FRONT);
        mOpenCvCameraView.setWindowManager(getWindowManager());


        mSaveTextView = (TextView) findViewById(R.id.face_save);
        if (mFaceOpened) {
            mSaveTextView.setText("取消");
        }
        mSaveTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mFaceOpened) {
                    mOpenCvCameraView.setCaptureFace();
                }
            }
        });


        mOpenCvCameraView.setICompareListener(new ICompareListener() {
            @Override
            public void onCompare(CompareResult result) {
                Log.e("FaceDetctActivity", "55555555555555");
                Log.i("onCompare", "result:" + (result == null ? "null" : result.getNum()));

                if (result != null && result.getLabel() == 1) {
                    if (mFaceResultListener != null) {
                        mFaceResultListener.faceDetcetOk();
                    }
                    Toast.makeText(mContext,"认证成功",Toast.LENGTH_SHORT).show();
                    goMain();
                } else {
                    if (mFaceResultListener != null) {
                        mFaceResultListener.faceDetcetError();
                    }
                    Toast.makeText(mContext,"认证失败，请重新登录",Toast.LENGTH_SHORT).show();

                }
            }

            @Override
            public void onCapture(boolean isTrue) {

                Toast.makeText(mContext, isTrue ? "采集成功" : "采集失败",Toast.LENGTH_LONG).show();
                SharedPreferences.Editor editor = sp.edit();
                editor.putBoolean("open",true);
                editor.commit();
                finish();

            }
        });

        System.loadLibrary("opencv_java3");
        System.loadLibrary("detection_based_tracker_jni");
    }

    private void goMain() {
        Intent intent = new Intent(FdVR1NDActivity.this, MainActivity.class);
        startActivity(intent);
    }

    @Override
    public void onResume() {
        super.onResume();
        init();
        Log.e( "FaceDetctActivity", "44444444444444444444");
    }

    private void init() {
        mOpenCvCameraView.enableView();
        mOpenCvCameraView.setCompareFace(true);

    }

    private void recognityOutTime() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (mOpenCvCameraView != null) {
                            Toast.makeText(mContext,  "认证超时，请重试",Toast.LENGTH_LONG).show();
                        }
                    }
                });

            }
        }, 5 * 1000);
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mOpenCvCameraView != null) {
            mOpenCvCameraView.disableView();
            mOpenCvCameraView = null;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mOpenCvCameraView != null) {
            mOpenCvCameraView.disableView();
            mOpenCvCameraView = null;
        }
    }
}
